package org.elasticsearch.index.analysis;

import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.lucene.IKTokenizer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class IkTokenizerFactory extends AbstractTokenizerFactory {
    private final static Logger logger = LogManager.getLogger(IkTokenizerFactory.class);
    private Configuration configuration;

    public IkTokenizerFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        super(indexSettings, name, settings);
        configuration=new Configuration(env,settings);
    }

    public static IkTokenizerFactory getIkTokenizerFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new IkTokenizerFactory(indexSettings,env, name, settings).setSmart(false);
    }

    public static IkTokenizerFactory getIkSmartTokenizerFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new IkTokenizerFactory(indexSettings,env, name, settings).setSmart(true);
    }

    public IkTokenizerFactory setSmart(boolean smart){
        this.configuration.setUseSmart(smart);
        return this;
    }

    @Override
    public Tokenizer create() {
        logger.warn("Create ik tokenizer");
        return new IKTokenizer(configuration);  }
}
